﻿using DreamCMS.BLL;
using DreamCMS.Entity;
using DreamCMS.Entity.Common;
using DreamCMS.Web.Admin;
using DreamCMS.WebCore.Common;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Text;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DreamCMS.Web.admin
{
    public partial class setting : AdminPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindSetting();
            }
            if(Request.QueryString["msg"]=="success")
            {
                ClientScript.RegisterStartupScript(GetType(), "successAdd", "<script>alert('添加成功');</script>");
            }
        }



        /// <summary>
        /// 绑定
        /// </summary>
        protected void BindSetting()
        {
            SiteSettingInfo s = SiteSettingManager.GetSiteSettingInfo();
            if (s != null)
            {
                txtSiteName.Text = StringHelper.HtmlDecode(s.SiteName);
                txtSiteDescription.Text = StringHelper.HtmlDecode(s.SiteDescription);
                txtMetaKeywords.Text = StringHelper.HtmlDecode(s.MetaKeywords);
                txtMetaDescription.Text = StringHelper.HtmlDecode(s.MetaDescription);

                chkSiteStatus.Checked = s.TheSiteStatus == SiteStatus.OpenIt ? true : false;

                chkEnableVerifyCode.Checked = s.EnableVerifyCode == SiteVerify.OpenIt ? true : false;


                txtSidebarPostCount.Text = s.SidebarPostCount.ToString();
                txtSidebarCommentCount.Text = s.SidebarCommentCount.ToString();
                txtSidebarTagCount.Text = s.SidebarTagCount.ToString();

                txtPageSizeCommentCount.Text = s.PageSizeCommentCount.ToString();
                txtPageSizePostCount.Text = s.PageSizePostCount.ToString();

                txtPostRelatedCount.Text = s.PostRelatedCount.ToString();

                txtFooterHtml.Text = s.FooterHtml;

                //评论
                chkCommentStatus.Checked = s.CommentStatus == PostCommentStatus.Open ? true : false;
                ddlCommentApproved.SelectedValue = ((long)s.CommentApproved).ToString();
                txtCommentSpamwords.Text = s.Rubbish;


                //rewrite
                ddlRewriteExtension.SelectedValue = s.RewriteExtension;
                //total
                ddlTotalType.SelectedValue = ((long)s.GuestRecordType).ToString();

                ddlPostShowType.SelectedValue = ((long)s.PostShowType).ToString();

            }

        }



        protected void btnEdit_Click(object sender, EventArgs e)
        {
            SiteSettingInfo s = SiteSettingManager.GetSiteSettingInfo();
            if (s != null)
            {
                s.SiteName = StringHelper.HtmlEncode(txtSiteName.Text);
                s.SiteDescription = StringHelper.HtmlEncode(txtSiteDescription.Text);
                s.MetaKeywords = StringHelper.HtmlEncode(txtMetaKeywords.Text);
                s.MetaDescription = StringHelper.HtmlEncode(txtMetaDescription.Text);

                s.TheSiteStatus = chkSiteStatus.Checked ? SiteStatus.OpenIt : SiteStatus.Closed;

                s.EnableVerifyCode = chkEnableVerifyCode.Checked ? SiteVerify.OpenIt : SiteVerify.Closed;

                s.PageSizePostCount = StringHelper.StrToInt32(txtPageSizePostCount.Text, 10);
                s.PageSizeCommentCount = StringHelper.StrToInt32(txtPageSizeCommentCount.Text, 50);
                s.PostRelatedCount = StringHelper.StrToInt32(txtPostRelatedCount.Text.Trim(),0);
                s.SidebarPostCount = StringHelper.StrToInt32(txtSidebarPostCount.Text, 10);
                s.SidebarTagCount = StringHelper.StrToInt32(txtSidebarTagCount.Text, 10);
                s.SidebarCommentCount = StringHelper.StrToInt32(txtSidebarCommentCount.Text, 10);
                s.RewriteExtension = ddlRewriteExtension.SelectedValue;
                s.FooterHtml = txtFooterHtml.Text;

                //评论
                s.CommentStatus = chkCommentStatus.Checked ? PostCommentStatus.Open : PostCommentStatus.Close;
                s.CommentApproved = (SiteCommentApproveType)StringHelper.StrToInt64(ddlCommentApproved.SelectedValue, 1);
                s.Rubbish = txtCommentSpamwords.Text;
                

                //total
                s.GuestRecordType = (SiteRecordType)StringHelper.StrToInt32(ddlTotalType.SelectedValue, 1);

                s.PostShowType = (ContentShowType)StringHelper.StrToInt32(ddlPostShowType.SelectedValue, 2);

                if (SiteSettingManager.UpdateSettingInfo())
                {
                    Response.Redirect("setting.aspx?msg=success");
                }
            }
        }
    }
}